package com.xz.coursearrange.controller;


import com.xz.coursearrange.common.ServerResponse;
import com.xz.coursearrange.entity.CoursePlan;
import com.xz.coursearrange.entity.request.unUseClassroomRequest;
import com.xz.coursearrange.entity.request.unUseClassroomUpdateRequest;
import com.xz.coursearrange.entity.request.updateCoursePlanRequest;
import com.xz.coursearrange.service.CoursePlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 */
@RestController
@Validated
public class CoursePlanController {

    @Resource
    private CoursePlanService coursePlanService;

    /**
     * 获取所有学期
     */
    @GetMapping("/allsemester")
    public ServerResponse queryAllSemester() {
        return coursePlanService.queryAllSemester();
    }

    /**
     * 根据班级查询课程表
     */
    @GetMapping("/courseplan/{classno}")
    public ServerResponse queryCoursePlanByClassNo(@PathVariable("classno") @NotBlank String classNo) {
        return coursePlanService.queryCoursePlanByClassNo(classNo);
    }

    /**
     * 根据讲师编号课程表
     */
    @GetMapping("/courseplanTeacherNo/{teacherNo}")
    public ServerResponse queryCoursePlanByTeacherNo(@PathVariable("teacherNo") @NotBlank String teacherNo) {
        return coursePlanService.queryCoursePlanByTeacherNo(teacherNo);
    }

    /**
     * 根据教室编号查询课程表
     * @param classroomNo
     * @return
     */
    @GetMapping("/courseplanClassroomNo/{classroomNo}")
    public ServerResponse queryCoursePlanByClassroomNo(@PathVariable("classroomNo") @NotBlank String classroomNo) {
        return coursePlanService.queryCoursePlanByClassroomNo(classroomNo);
    }

    /**
     * 获取所有课程计划
     * @return
     */
//    @GetMapping("/courseplanall")
//    public ServerResponse queryCoursePlanAll() {
//        List<CoursePlan> list = coursePlanService.redlist();
//        return ServerResponse.ofSuccess(list);
//    }

    /**
     * 课程计划的删除
     * @return
     */
    @DeleteMapping("/courseplan/delete/{id}")
    public ServerResponse deleteCoursePlanById(@PathVariable("id") @NotNull Integer id) {
        boolean b = coursePlanService.removeById(id);
        if (b){
            return ServerResponse.ofSuccess("删除成功");
        }else {
            return ServerResponse.ofError("删除失败");
        }
    }

    /**
     * 获取未被选择的时间
     * @param id
     * @return
     */
    @PostMapping("/courseplan/unuseclasstime/{id}")
    public ServerResponse unUseClassTime(@PathVariable("id") @NotNull Integer id) {
        return coursePlanService.unUseClassTime(id);
    }

    /**
     * 获取可以选择的教室
     */
    @PostMapping("/courseplan/unuseclassroom")
    public ServerResponse unUseClassroom(@RequestBody @Valid unUseClassroomRequest request){
        if (request == null){
            return ServerResponse.ofError("参数错误");
        }
        Integer id = request.getId();
        String classTime = request.getClassTime();
        if (classTime == null){
            return ServerResponse.ofError("参数错误");
        }
        if (id == null){
            return ServerResponse.ofError("参数错误");
        }
        return coursePlanService.unUseClassroom(id, classTime);
    }

    /**
     * 更新修改的数据
     */
    @PutMapping("/courseplan/update")
    public ServerResponse updateCoursePlan(@RequestBody updateCoursePlanRequest request) {
        if (request == null){
            return ServerResponse.ofError("参数错误");
        }

        CoursePlan coursePlane = new CoursePlan();
        BeanUtils.copyProperties(request, coursePlane);
        return coursePlanService.updateById(coursePlane) ? ServerResponse.ofSuccess("修改成功") : ServerResponse.ofError("修改失败");
    }


    /**
     * 获取该班级未被使用的时间
     * @param classNo
     * @return
     */
    @PostMapping("/courseplan/unuseclasstimeByClassNo/{classNo}")
    public ServerResponse unUseClassTimeByClassNo(@PathVariable("classNo") @NotBlank String classNo) {
        return coursePlanService.unUseClassTimeByClassNo(classNo);
    }

    /**
     * 获取该时间段未上课的所有老师
     */
    @PostMapping("/courseplan/unuseteacherByClassTime/{classTime}")
    public ServerResponse unUseTeacherByClassTime(@PathVariable("classTime") @NotBlank String classTime) {
        return coursePlanService.unUseTeacherByClassTime(classTime);
    }

    /**
     * 获取该时间段 该课程属性 未上课的所有教室 class_time
     */
    @PostMapping("/courseplan/unUseClassroomByClassTimeAndCourseAttr")
    public ServerResponse unUseClassroomByClassTimeAndCourseAttr(@RequestBody @Valid unUseClassroomUpdateRequest request){
        if (request == null){
            return ServerResponse.ofError("参数错误");
        }
        String classTime = request.getClassTime();
        String courseNo = request.getCourseNo();
        if (classTime == null || courseNo == null){
            return ServerResponse.ofError("参数错误");
        }

        return coursePlanService.unUseClassroomByClassTimeAndCourseAttr(courseNo, classTime);
    }

    /**
     * 保存课程计划
     * @return
     */
    @PostMapping("/courseplan/addCoursePlan")
    public ServerResponse addCoursePlan(@RequestBody @Valid CoursePlan coursePlan){
        boolean save = coursePlanService.save(coursePlan);
        if (save){
            return ServerResponse.ofSuccess("添加成功");
        }else{
            return ServerResponse.ofError("添加失败");
        }
    }
}

